﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.MVC.Classes;
using KPIS.GERP.SERVICE;
using KPIS.GERP.SERVICE.DAL;
using KPIS.GERP.SERVICE.Model;

namespace KPIS.GERP.MVC.Controllers
{
    public class PrepareRuleApproveController : Controller
    {
        private SERVICE.DAL.RuleApprove dal = new KPIS.GERP.SERVICE.DAL.RuleApprove();

        public ActionResult Index()
        {
            try
            {
                rule_approve_group g = new rule_approve_group();
                g.rule_approve_group_seq = 4001;
                g.rule_approve_group_name = "ขออนุมัติการแจ้งความประสงค์";
                g.ref_to_database = "SqlServer";
                g.ref_to_table_name = "PrePurchaseRequest";
                g.ref_column_seq_name = "PprId";
                g.ref_action_name = "Details";
                g.ref_controller_name = "PrePurchase";
                g.created_by = new SsMng().EmpSeqI;

                rule_approve_type t = new rule_approve_type();
                t.rule_approve_type_seq = 4000;
                t.rule_approve_group_seq = 4001;
                t.rule_approve_type_name = "ขออนุมัติแจ้งความประสงค์";
                t.remarks = "สร้างที่ PrepareRuleApproveController";
                t.created_by = new SsMng().EmpSeqI;

                var la = new List<rule_approve>();
                rule_approve r1 = new rule_approve();
                r1.step = 1;
                r1.approver_type = "1";
                r1.approver_seq = 1;
                r1.action = " J,A";
                r1.record_status = "U";
                r1.created_by = 1;

                la.Add(r1);

                CreateRuleApprove(g, t, la);
                ViewBag.message = "OK";

            } catch(Exception error)
            {
                ViewBag.message = "Error as followed: " + error.Message + ", " + (error.InnerException == null ? string.Empty : error.InnerException.Message);
            }

            return View();
        }

        private void CreateRuleApprove(rule_approve_group ruleApproveGroup, rule_approve_type ruleApproveType, IEnumerable<rule_approve> ruleApproves)
        {
            string ruleApproveGroupSeq = ruleApproveGroup.rule_approve_group_seq.ToString();
            string ruleApproveTypeSeq = ruleApproveType.rule_approve_type_seq.ToString();

            var ruleApproveGroupDb = dal.GetRuleApproveGroup(ruleApproveGroupSeq).FirstOrDefault();
            var ruleApproveTypeDb = dal.GetRuleApproveType(ruleApproveTypeSeq, null, false).FirstOrDefault();

            // 1. Prepare group
            if (ruleApproveGroupDb == null)
            {
                ruleApproveGroupSeq = dal.InsertRuleApproveGroup(ruleApproveGroup.rule_approve_group_name,
                                           ruleApproveGroup.remarks, "U",
                                           Convert.ToString(ruleApproveGroup.created_by), 
                                           Convert.ToString(ruleApproveGroup.rule_approve_group_seq));

            } else
            {
                ruleApproveGroupSeq = ruleApproveGroup.rule_approve_group_seq.ToString();
                dal.UpdateRuleApproveGroup(ruleApproveGroupSeq,
                    ruleApproveGroupDb.rule_approve_group_name,
                    ruleApproveGroupDb.remarks, "U",
                    Convert.ToString(ruleApproveGroupDb.created_by),
                    ruleApproveGroupDb.ref_to_database,
                    ruleApproveGroupDb.ref_to_table_name,
                    ruleApproveGroupDb.ref_column_seq_name,
                    ruleApproveGroupDb.ref_action_name,
                    ruleApproveGroupDb.ref_controller_name
                    );
            }

            // 2. Prepare type 
            if (ruleApproveTypeDb == null)
            {
                ruleApproveTypeSeq = dal.InsertRuleApproveType(
                    ruleApproveGroupSeq,
                    ruleApproveType.rule_approve_type_name,
                    ruleApproveType.remarks, "U",
                    Convert.ToString(ruleApproveType.created_by));

            }
            else
            {
                ruleApproveTypeSeq = ruleApproveType.rule_approve_type_seq.ToString();
                dal.UpdateRuleApproveType(
                    ruleApproveType.rule_approve_type_seq.ToString(),
                    ruleApproveGroupSeq,
                    ruleApproveType.rule_approve_type_name,
                    ruleApproveType.remarks, "U",
                    Convert.ToString(ruleApproveType.created_by));
            }

            // 3. Prepare rule_approve
            foreach (var ruleApprove in ruleApproves)
            {
                var ruleApproveOfStepDb = dal.GetRuleApprove(null, ruleApproveTypeSeq, false, false, false, ruleApprove.step).FirstOrDefault();
                if (ruleApproveOfStepDb == null)
                {
                    dal.InsertRuleApprove(ruleApproveTypeSeq, ruleApproveOfStepDb.step.ToString(),
                                                                       ruleApproveOfStepDb.approver_type, ruleApproveOfStepDb.approver_seq.ToString(),
                                                                       ruleApproveOfStepDb.action, ruleApproveOfStepDb.remarks,
                                                                       "U", ruleApproveOfStepDb.created_by.ToString());
                }
                else
                {
                    dal.UpdateRuleApprove(ruleApprove.rule_approve_seq.ToString(), ruleApproveTypeSeq, 
                        ruleApprove.step.ToString(),
                                          ruleApprove.approver_type, ruleApprove.approver_seq.ToString(),
                                          ruleApprove.action, ruleApprove.remarks,
                                          "U", ruleApprove.created_by.ToString());
                }
            }
        }

    }
}